﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class frmas3xinzeng : Form
    {
        public frmas3xinzeng()
        {
            InitializeComponent();
        }
        string str = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
       
        private void button1_Click(object sender, EventArgs e)
        {
if(yanzheng()){
    add();
    this.Close();
}
        }
        //验证
        public bool yanzheng() { 
        if(this.txtbianhao.Text.Trim().Equals(string .Empty)){
            this.txtbianhao.Focus();
            MessageBox.Show("单位编号");
            return false;
        }if(this.txtquanname.Text.Trim().Equals(string.Empty)){
            this.txtquanname.Focus();
            MessageBox.Show("单位全程不能为空");
            return false;

        }if(this.txtname.Text.Trim().Equals(string.Empty)){
            this.txtname.Focus();
            MessageBox.Show("联系人不能为空");
            return false;
        }if(this.txtphone.Text.Trim().Equals(string.Empty)){
            this.txtphone.Focus();
            MessageBox.Show("联系电话不能为空");
            return false;
        }if(this.txtaddp.Text.Trim().Equals(string.Empty)){
            this.txtaddp.Focus();
            MessageBox.Show("联系电话不能为空");
            return false;
        } if (this.txtbeizhu.Text.Trim().Equals(string.Empty))
        {
            this.txtbeizhu.Focus();
            MessageBox.Show("备注信息不能为空");
            return false;

        }
        else {
            return true;
        }
        }
        //新增
        public void add() {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string qw1 = this.txtbianhao.Text;
            string qw2 = this.txtquanname.Text;
            string qw3 = this.txtname.Text;
            string qw4 = this.txtphone.Text;
            string qw5 = this.txtaddp.Text;
            string qw6 = this.txtbeizhu.Text;
            try
            {
                string sql = "insert into dbo.jinguocaigou values('" + qw1 + "','" + qw2 + "','" + this.txtjian.Text + "','" + qw3 + "','" + qw4 + "','" + qw5 + "','" + qw6 + "')";
                SqlCommand comm = new SqlCommand(sql, conn);
                int i = comm.ExecuteNonQuery();
                if (i > 0)
                {
                    MessageBox.Show("添加成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                conn.Close();
            }
        }
        //关闭
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
